import {ProcedureBase} from "../../AzraelFrameWork/src/Procedure/ProcedureBase";
import {GameMode} from "../Game/GameMode";
import {BaseGame} from "../Game/BaseGame";
import {IFsm} from "../../AzraelFrameWork/src/Fsm/IFsm";
import {IProcedureManager} from "../../AzraelFrameWork/src/Procedure/IProcedureManager";
import {NormalGame} from "../Game/NormalGame";
import {Constant} from "../Definition/Constant";
import {UIStruct} from "../UI/UIStruct";
import GameMainEntry from "../Base/GameMainEntry";
import ccclass = cc._decorator.ccclass;
import {ProcedureMain} from "./ProcedureMain";
import {EventID} from "../Event/EventID";
import ActionInterval = cc.ActionInterval;
type ProcedureOwner = IFsm<IProcedureManager>;
@ccclass("ProcedureStart")
export  class ProcedureStart extends ProcedureBase{
    private readonly _UINames:Map<UIStruct,string> = new Map<UIStruct, string>();
    private _isChange:boolean;

    OnInit(fsm: ProcedureOwner): void {
        super.OnInit(fsm);
        this._UINames.set(UIStruct.UI_Start, "ui_start");
        this._UINames.set(UIStruct.UI_fight, "ui_zhandou");
        GameMainEntry.UI.Init(this._UINames);
    }

    OnEnter(fsm: ProcedureOwner): void {
        super.OnEnter(fsm);
        GameMainEntry.Event.Subscribe(EventID.ChangeScene,this.ChangeGameScene.bind(this));
        GameMainEntry.UI.OpenUIForm(UIStruct.UI_Start);
    }

    OnUpdate(fsm: ProcedureOwner, elapseSeconds: number, realElapseSeconds: number): void {
        super.OnUpdate(fsm, elapseSeconds, realElapseSeconds);
        if(this._isChange){
            this.ChangeState(fsm,ProcedureMain);
        }
    }

    OnLeave(fsm: ProcedureOwner, isShutdown: boolean): void {
        super.OnLeave(fsm, isShutdown);
        GameMainEntry.Event.Unsubscribe(EventID.ChangeScene,this.ChangeGameScene.bind(this));
        this._isChange = false;
    }

    OnDestroy(fsm: ProcedureOwner): void {
        super.OnDestroy(fsm);
    }

    ChangeGameScene(sender,e){
        cc.director.loadScene("Game",(err,afterscene:cc.Scene)=>{
            if(err){
                cc.error(err);
                return;
            }
            this._isChange = true;
            GameMainEntry.UI.CloseUIForm(UIStruct.UI_Start);
        });
    }

}